﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ConduitSJAndPYNew : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new NotImplementedException();
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				string text = "升降偏移";
				transaction = new Transaction(document, text);
				transaction.Start();
				RevitVersionFuncs.ClearSelection(selection);
				ISelectionFilter selectionFilter = new ConduitSelectionFilter();
				FormRoundBeamUseHand formRoundBeamUseHand = new FormRoundBeamUseHand(Math.PI*.5);
				if (formRoundBeamUseHand.ShowDialog() == DialogResult.OK)
				{
					double distance = formRoundBeamUseHand.distance;
					Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择要升降或者偏移的管道,点击点为切割位置点");
					Conduit conduit = document.GetElement(reference) as Conduit;
					HYConduit hyconduit = new HYConduit(conduit);
					XYZ globalPoint = reference.GlobalPoint;
					if (formRoundBeamUseHand.model == "单侧")
					{
						YJKLine hyline = new YJKLine(hyconduit.StartXYZ, hyconduit.EndXYZ);
						XYZ xyz = null;
						for (;;)
						{
							xyz = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请在管道上选择一点确定升降或者偏移的方向").GlobalPoint;
							if (new XYZ(xyz.X, xyz.Y, globalPoint.Z).DistanceTo(globalPoint) >= 0.065616797900262466)
							{
								break;
							}
							YJKMessageBox.Show("选择的方向点与切割位置点过于接近，无法判断方向，请重新选择方向点！");
						}
						HYCableTrayRoundBeam hycableTrayRoundBeam = new HYCableTrayRoundBeam();
						hycableTrayRoundBeam.NewPipePIANYI(conduit, globalPoint, formRoundBeamUseHand.distance, formRoundBeamUseHand.angle, formRoundBeamUseHand.SJORPY, xyz);
						for (;;)
						{
							try
							{
								Reference reference2 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择需自动调整的平行管道");
								Conduit conduit2 = document.GetElement(reference2) as Conduit;
								HYConduit hyconduit2 = new HYConduit(conduit2);
								YJKLine hyline2 = new YJKLine(hyconduit2.CustomConnectorStart.XYZ, hyconduit2.CustomConnectorEnd.XYZ);
								XYZ xyz2 = hyconduit2.GetProject(globalPoint, true);
								XYZ project = hyconduit2.GetProject(xyz, true);
								if (hyline2.IsParallel(hyline.Line))
								{
									if (formRoundBeamUseHand.SJORPY == "偏移")
									{
										HYCreatePipeHelper hycreatePipeHelper = new HYCreatePipeHelper();
										formRoundBeamUseHand.distance = distance;
										double num = hyline.Line.Distance(xyz2);
										if (distance > 0.0)
										{
											if (hycreatePipeHelper.judgePointIsAnotherLine(hyline.Line, hyconduit2.CustomConnectorStart.XYZ))
											{
												xyz2 += (xyz2 - project).Normalize() * num;
											}
											else
											{
												xyz2 -= (xyz2 - project).Normalize() * num;
											}
										}
										else if (hycreatePipeHelper.judgePointIsAnotherLine(hyline.Line, hyconduit2.CustomConnectorStart.XYZ))
										{
											xyz2 -= (xyz2 - project).Normalize() * num;
										}
										else
										{
											xyz2 += (xyz2 - project).Normalize() * num;
										}
									}
									if (hyconduit2.IsPointOnDuctCurve(xyz2) && hyconduit2.IsPointOnDuctCurve(project))
									{
										hycableTrayRoundBeam.NewPipePIANYI(conduit2, xyz2, formRoundBeamUseHand.distance, formRoundBeamUseHand.angle, formRoundBeamUseHand.SJORPY, project);
									}
									else
									{
										YJKMessageBox.Show("切割点不在该管道上，无法复制升降或偏移！");
									}
								}
								else
								{
									YJKMessageBox.Show("该管道不平行！");
								}
								continue;
							}
							catch (Autodesk.Revit.Exceptions.OperationCanceledException)
							{
							}
							break;
						}
					}
					if (formRoundBeamUseHand.model == "两侧")
					{
						XYZ xyz3 = null;
						Reference reference3;
						for (;;)
						{
							reference3 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择要升降或者偏移的管道,点击点为切割位置点二");
							xyz3 = reference3.GlobalPoint;
							if (xyz3.DistanceTo(globalPoint) >= 0.065616797900262466)
							{
								break;
							}
							YJKMessageBox.Show("选择的切割位置点二与首个切割位置点过于接近，请重新选择切割位置点二！");
						}
						Conduit conduit3 = document.GetElement(reference3) as Conduit;
						HYConduit hyconduit3 = new HYConduit(conduit3);
						YJKLine hyline3 = new YJKLine(hyconduit3.CustomConnectorStart.XYZ, hyconduit3.CustomConnectorEnd.XYZ);
						HYCableTrayRoundBeam hycableTrayRoundBeam2 = new HYCableTrayRoundBeam();
						if (conduit.Id == conduit3.Id)
						{
							hycableTrayRoundBeam2.NewPipePIANYI(conduit, globalPoint, xyz3, formRoundBeamUseHand.distance, formRoundBeamUseHand.angle, formRoundBeamUseHand.SJORPY, false);
							for (;;)
							{
								try
								{
									Reference reference4 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择需自动调整的平行管道");
									Conduit conduit4 = document.GetElement(reference4) as Conduit;
									HYConduit hyconduit4 = new HYConduit(conduit4);
									YJKLine hyline4 = new YJKLine(hyconduit4.CustomConnectorStart.XYZ, hyconduit4.CustomConnectorEnd.XYZ);
									XYZ xyz4 = hyconduit4.GetProject(globalPoint, true);
									XYZ xyz5 = hyconduit4.GetProject(xyz3, true);
									if (hyline3.IsParallel(hyline4.Line))
									{
										if (formRoundBeamUseHand.SJORPY == "偏移")
										{
											HYCreatePipeHelper hycreatePipeHelper2 = new HYCreatePipeHelper();
											formRoundBeamUseHand.distance = distance;
											double num2 = hyline3.Line.Distance(xyz4);
											if (distance > 0.0)
											{
												if (hycreatePipeHelper2.judgePointIsAnotherLine(hyline3.Line, hyconduit4.CustomConnectorStart.XYZ))
												{
													xyz4 += (xyz4 - xyz5).Normalize() * num2;
													xyz5 += (xyz5 - xyz4).Normalize() * num2;
												}
												else
												{
													xyz4 -= (xyz4 - xyz5).Normalize() * num2;
													xyz5 -= (xyz5 - xyz4).Normalize() * num2;
												}
											}
											else if (hycreatePipeHelper2.judgePointIsAnotherLine(hyline3.Line, hyconduit4.CustomConnectorStart.XYZ))
											{
												xyz4 -= (xyz4 - xyz5).Normalize() * num2;
												xyz5 -= (xyz5 - xyz4).Normalize() * num2;
											}
											else
											{
												xyz4 += (xyz4 - xyz5).Normalize() * num2;
												xyz5 += (xyz5 - xyz4).Normalize() * num2;
											}
										}
										if (hyconduit4.IsPointOnDuctCurve(xyz4) && hyconduit4.IsPointOnDuctCurve(xyz5))
										{
											hycableTrayRoundBeam2.NewPipePIANYI(conduit4, xyz4, xyz5, formRoundBeamUseHand.distance, formRoundBeamUseHand.angle, formRoundBeamUseHand.SJORPY, false);
										}
										else
										{
											YJKMessageBox.Show("切割点不在该管道上，无法复制升降或偏移！");
										}
									}
									else
									{
										YJKMessageBox.Show("该管道不平行！");
									}
									continue;
								}
								catch (Autodesk.Revit.Exceptions.OperationCanceledException)
								{
									goto IL_64C;
								}
								break;
							}
						}
						if (conduit.Id != conduit3.Id)
						{
							YJKMessageBox.Show("线管升降不支持带附件的升降！");
						}
					}
				}
				IL_64C:
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (1 == (int)transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
